home *** CD-ROM | disk | FTP | other *** search
- Path: noc.netcom.net!news
- From: Tarang Deshpande <tarang@willows.com>
- Newsgroups: comp.lang.c
- Subject: Re: Arrays of strings (linking)
- Date: Wed, 27 Mar 1996 18:31:08 -0800
- Organization: NETCOM Network Operations
- Message-ID: <3159F9EC.1835@willows.com>
- References: <4jbkpg$5m7@news.ariadne-t.gr>
- NNTP-Posting-Host: daffy.willows.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB2 (Win95; I)
-
- Arnellos Argiris-TEIA wrote:
- >
- > I have problem writing a function that accepts two strings and then linking
- > them together in a new string. I must also return a pointer to this new
- > string. For example: if I pass "Hello" to string1 and "World" to string2
- > the function must return a pointer to "Hello World!".
- > Could anyone help me by giving me an example or any ideas.
- >
- > TIA
- > Argiris Arnellos
-
- If you don't mind string1 being modified you could just use strcat
- otherwise use the following code. Note that this function requires
- that the returned pointer be freed.
-
- char* strjoin ( char *str1, char* str2 )
- {
-
- char *str = malloc ( strlen ( str1 ) + strlen ( str2 ) + 1 );
-
- if ( str )
- {
- *str = '\0';
- strcat ( str, str1 );
- strcat ( str, str2 );
- }
-
- return ( str );
-
- }
-